package com.vclee.fast.utils.exception;

import java.util.regex.Pattern;

public class Assert {

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isBlank(String str){
        return str == null || str.isEmpty();
    }

    public static void isBlank(String str, String message) {
        if(isBlank(str)) throw new RRException(message);
    }

    public static boolean notBlank(String str){
        return !isBlank(str);
    }

    public static boolean isNull(Object object){
        return  object == null;
    }

    public static void isNull(Object object, String message) {
        if(object == null) throw new RRException(message);
    }

    public static boolean notNull(Object object){
        return !isNull(object);
    }

    public static void claim(Boolean result, String message) {
        if(result) throw new RRException(message);
    }

    public static void claim(Boolean result,Integer code, String message){
        if(result) throw new RRException(message,code);
    }

    public static boolean verifyStrLen(String target,int targetLen){
        return target != null && target.length() ==targetLen;
    }

    public static void verifyStrLen(String target,int targetLen,String msg){
        Assert.claim(!verifyStrLen(target,targetLen),msg);
    }

    public static boolean verifyStrLen(String target,int minLen,int maxLen){
        return target != null && target.length()>minLen && target.length()<=maxLen;
    }

    public static void verifyStrLen(String target,int minLen,int maxLen,String msg){
        Assert.claim(!verifyStrLen(target,minLen,maxLen),msg);
    }


    public static boolean verifyNumber(Number target,Number min,Number max){
        return target != null && target.doubleValue()>=min.doubleValue() && target.doubleValue()<=max.doubleValue();
    }

    public static void verifyNumber(Number target,Number min,Number max,String msg){
        Assert.claim(!verifyNumber(target,min,max),msg);
    }

    public static boolean verifyUrl(String target){
        return Pattern.matches("^(?:(http[s]?):)?//([^:/\\?]+)(?::(\\d+))?([^\\?]*)\\??(.*)",target);
    }

    public static void verifyUrl(String target,String msg){
        Assert.claim(!verifyUrl(target),msg);
    }


    private static final String qqReg = "\"[1-9][0-9]{4,14}\""; // 8到16位,数字字母大小写
    private static final String passReg = "^(?![^a-zA-Z]+$)(?!\\D+$).{8,16}$"; // 8到16位,数字字母大小写
    private static final String phoneReg = "^[1]\\d{10}$"; // "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$"
    private static final String emailReg = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$"; // "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$"

    public static boolean isPwd(String password){
        return password != null && password.matches(passReg);
    }

    public static boolean notPwd(String password){
        return password == null || !password.matches(passReg);
    }

    public static boolean isPhone(String phone){
        return phone!= null && phone.matches(phoneReg);
    }

    public static boolean notPhone(String phone){
        return phone == null || !phone.matches(phoneReg);
    }

    public static boolean isEmail(String email){
        return email != null && email.matches(emailReg);
    }

    public static boolean notEmail(String email){
        return email == null || !email.matches(emailReg);
    }

    public static boolean isQq(String qq){
        return qq!=null && qq.matches(qqReg);
    }

    public static boolean notQq(String qq){
        return qq==null || !qq.matches(qqReg);
    }

    public static boolean isCardNo(String cardNo) {
        if(Assert.isBlank(cardNo)) return false;
        if(cardNo.length()==15) return true;
        if (cardNo.length() != 18) return false;
        //校验身份证真假
        int sum = 0;
        int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//将加权因子定义为数组
        //遍历weight数组 求身份证前17项系数和
        for (int i = 0; i < weight.length; i++) {
            int n = cardNo.charAt(i) - 48;//获取 身份证对应数
            int w = weight[i];
            sum += w * n;
        }
        int index = sum % 11;  //对11求余
        String m = "10X98765432"; // 校验码
        return m.charAt(index) == cardNo.charAt(17); // 获取身份证最后一位进行比对
    }

    public static boolean notCardNo(String cardNo) {
        return !isCardNo(cardNo);
    }
}
